<template>
  <div>
    <el-container>
      <el-header height="70px">
        <p class="header_title">角色管理</p>
        <!--  -->
        <el-breadcrumb separator="/">
          <el-breadcrumb-item>权限维护</el-breadcrumb-item>
          <el-breadcrumb-item>角色管理</el-breadcrumb-item>
          <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>
      </el-header>

      <el-main>
        <el-card class="box-card">
          <div slot="header">
            <span>角色列表</span>
            <span style="float: right;">
              <i class="el-icon-arrow-up"></i>
              <i class="el-icon-close"></i>
            </span>
          </div>

          <!--  -->
          <el-row :gutter="20">
            <el-col :span="12">
              <el-input
                placeholder="根据名称查询"
                v-model="rolelist.name"
                clearable
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="search"
                ></el-button>
              </el-input>
            </el-col>
            <el-col :span="4">
              <el-button type="primary" class="add"  @click="addDialogVisible = true"
                >添加</el-button
              >
            </el-col>
            <el-col :span="5">
                <b class="sum_data">数据总数：</b>
               <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pagination.pagenum"
                :page-size="pagination.pagesize"
                 :page-sizes="[3, 5, 10, 20]"
                layout="total, sizes"
                :total="pagination.total"
                class="top_nav"
                >
               </el-pagination>
            </el-col>
          </el-row>

          <!-- 表格 -->
          <el-table
            :data="rolelist"
            style="width: 100%"
            border
            stripe
            :row-class-name="tableRowClassName"
          >
            <el-table-column label="# " width="60px" type="expand">
              <p class="roleTitle">所属行为列表</p>
              <el-table  border stripe>
                <el-table-column label="名称" prop="name"></el-table-column>
              </el-table>
            </el-table-column>
            <el-table-column prop="alias" label="名称"> </el-table-column>
            <el-table-column prop="date" label="创建时间"> </el-table-column>
            <el-table-column prop="operate" label="操作">
              <template slot-scope="scope">
                <el-button type="primary" size="mini" @click="editbtn(scope.row)">编辑</el-button>
                <el-button type="danger" size="mini" @click="removeId(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pagination.pagenum"
            :page-sizes="[3, 5, 10, 20]"
            :page-size="pagination.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pagination.total"
          >
          </el-pagination>
        </el-card>
      </el-main>
    </el-container>

    <!-- 添加对话框 -->
    <el-dialog
      title="添加行为信息"
      :visible.sync="addDialogVisible"
      width="60%"
     @close="addDialogClosed">
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
        class="demo-ruleForm"
      >
        <el-form-item label="id号" prop="id">
          <el-input v-model="addForm.id" type="number"></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="alias">
          <el-input v-model="addForm.alias" type="text"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑对话框  -->

    
  </div>
</template>

<script>
export default {
  data() {
    return {
      roleInfo: {
        roles: "",
        total: 9,
      },
      rolelist: [],
      pagination: {
        pagenum: 1,
        pagesize: 3,
        total: 4,
      },
    //   添加角色
     addDialogVisible: false,
      addForm: {
        id: "",
        rolename: "",
      },
      addFormRules: {
        id: [{ required: true, message: "请输入id号", trigger: "blur" }],
        rolename: [
          { required: true, message: "请选择角色名称", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex === 1) {
        return "warning-row";
      } else if (rowIndex === 3) {
        return "success-row";
      }
      return "";
    },
    // /role/select
    async getRoleList() {
      const res = await this.http.get("/role/select", this.pagination);
      this.rolelist = res.roles;
      this.pagination.total = res.total;
      console.log(res);
    },
    // 监听pagesize的改变
    handleSizeChange(newSize) {
      // console.log(newSize);
      this.pagination.pagesize = newSize;
      this.getRoleList();
    },
    // 监听 页码值 改变
    handleCurrentChange(newPage) {
      // console.log(newPage);
      this.pagination.pagenum = newPage;
      this.getRoleList();
    },
     addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    onSubmit(){
        this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        // 发起添加网络请求
        const { res } = await this.http.post("/role/add", this.addForm);

        if (status !== 400) {
          this.$message.error("添加行为失败");
        }
        this.$message.success("添加行为成功");
        this.addDialogVisible = false;
       this.getRoleList();
      });
    },
        // 编辑按钮
    editbtn(row) {
      console.log(row.id);
      console.log(row.alias);
      this.$router.push({path:'/edit_role',query:{id:row.id}});
    },
    // 
    async removeId(row){
       console.log(row);
      const confirmResult = await this.$confirm(
        "此操作将永久删除该用户, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((error) => error);
      console.log(confirmResult);
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }

      const res = await this.http.post("/role/delete",row.id);
      console.log(res);
       this.getRoleList();
    },
    async search(){
      const res = await this.http.get('/role/selectByname',{
        name:this.rolelist.name,
        pagenum:this.pagination.pagenum,
        pagesize:this.pagination.pagesize
      });
      console.log(res);
      
    }

    
  },
  created() {
    this.getRoleList();
  },
};
</script>

<style scoped>
.add {
  margin-bottom: 20px;
  margin-left: 10px;
  width: 100px;
}
.el-input__inner {
  height: 25px;
}
.el-col {
  font-size: 14px;
  font-weight: 400;
  color: rgb(165, 164, 164);
}
/* expand */

.roleTitle {
  font-weight: bold;

  padding-bottom: 5px;
}
/* 隔行颜色效果 */
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>
